{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "traindata = pd.read_table('./horseColicTest.txt',header=None)\n",
    "testdata = pd.read_table('./horseColicTraining.txt',header=None)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Sigmoid(inx):\n",
    "    if inx>=0:     \n",
    "        return 1.0/(1+np.exp(-inx))\n",
    "    else:\n",
    "        return np.exp(inx)/(1+np.exp(inx))\n",
    "\n",
    "def classify(x):\n",
    "    if x>=0:\n",
    "        return 1.0\n",
    "    else :\n",
    "        return 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "def stoGradAscent1(data,classLabels,numIter=1000):\n",
    "    m,n = data.shape\n",
    "    weights = np.ones(n)\n",
    "    w_log = [weights]\n",
    "    for j in range(numIter):\n",
    "        dataIndex = list(range(m))\n",
    "        for i in range(m):\n",
    "            alpha = 4/(1.0+j+i)+0.01\n",
    "            randIndex = int(np.random.uniform(0,len(dataIndex)))\n",
    "            h = Sigmoid(np.dot(data[randIndex,:],weights))\n",
    "            error = classLabels[randIndex] - h\n",
    "            weights = weights + alpha * error * data[randIndex]\n",
    "            w_log.append(weights)\n",
    "            del(dataIndex[randIndex])\n",
    "    return weights,np.array(w_log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.array(traindata.iloc[:,:-1])\n",
    "data=np.c_[data,np.ones(data.shape[0])]\n",
    "classLabels = np.array(traindata.iloc[:,-1])\n",
    "weights,w_log = stoGradAscent1(data,classLabels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.33444816053511706"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testdataset,testlabel = np.c_[np.array(testdata.iloc[:,:-1]),np.ones(testdata.shape[0])],np.array(testdata.iloc[:,-1])\n",
    "res = np.dot(testdataset,weights)\n",
    "res =list(map(classify,res))\n",
    "(testlabel!=res).mean()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
